<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
    <form action="" method="get">
        <h3>生成二维码</h3>
        <div>
            <label for="content-input">内容</label>
            <input type="text" name="content" id="content-input" value="http://www.baidu.com">
        </div>
        <button type="button" onclick="encode()">生成</button>
    </form>
    <img src="" alt="" id="code-img">
    <script>
        function encode(){
            var content=$("#content-input").val();
            var url="index/encode?content=" + content;
            console.log(url);
            $("#code-img").attr("src",url);
        }
    </script>
    <form action="" method="post">
        <h3>解析二维码</h3>
        <div>
            <label for="img-input">二维码</label>
            <input type="file" name="img" id="img-input">
        </div>
        <button type="button" onclick="decode()">解析</button>
    </form>
    <p id="code-content"></p>
    <script>
        function decode(){
            var request = new XMLHttpRequest();
            
            fd = new FormData();
            var img=$("#img-input")[0].files[0];
            fd.append('img', img);
            
            var url="index/decode";
            request.open('POST', url, true);

            request.onreadystatechange = function() {
                if (this.readyState == 4) {
                    if (this.status >= 200 && this.status < 300) {
                        $("#code-content").text(this.response);
                    }
                }
            }
            request.send(fd);
        }
    </script>
</body>
</html>
